package com.eos.control.action.ajax;

import java.util.ArrayList;
import java.util.List;

import net.sf.json.JSONObject;

import com.eos.business.service.SysUserBusinessService;
import com.eos.common.tool.DataCacheUtil;
import com.eos.control.action.base.AbstractBaseAction;
import com.eos.orm.mybatis.model.RoleMappMenu;
import com.eos.orm.mybatis.model.SysRole;
import com.eos.orm.mybatis.model.ViewRoleMenu;
import com.hf.utils.StringUtil;

public class UpdateRoleAjax extends AbstractBaseAction {
	private static final long serialVersionUID = -5088299757545932551L;
	private Integer roleId;
	private String roleName;
	private String roleDesc;
	private String menuIds;
	private String roleStatus;
	private String result;
	
	public void setRoleId(Integer roleId) {
		this.roleId = roleId;
	}

	public void setRoleName(String roleName) {
		this.roleName = roleName;
	}

	public void setRoleDesc(String roleDesc) {
		this.roleDesc = roleDesc;
	}

	public void setMenuIds(String menuIds) {
		this.menuIds = menuIds;
	}

	public void setRoleStatus(String roleStatus) {
		this.roleStatus = roleStatus;
	}

	public String getResult() {
		return result;
	}

	@Override
	public String execute() {
		JSONObject json = null;
		try {
			json = new JSONObject();
			if (roleId == null || roleId.longValue() <= 0) {
				json.put("statusCode", "300");
				json.put("message", "参数错误, 修改角色失败!");
			} else {
				SysUserBusinessService sbs = new SysUserBusinessService();
				SysRole role = new SysRole();
				role.setId(roleId);
				role.setRoleName(roleName);
				role.setRoleDesc(roleDesc);
				role.setStatus(roleStatus);
				String exeStatus = sbs.updateSysRoleSelective(role);
				if("SUCCESS".equals(exeStatus)){
					//更新缓存中的角色信息
					DataCacheUtil.clearSysRoleMap();
					
					String[] menuIdArr = null;
					List<ViewRoleMenu> roleMenuList = DataCacheUtil.getSysMenuOfRole(roleId); //获取角色的菜单
					if(!StringUtil.isEmpty(menuIds)){
						menuIdArr = StringUtil.split(menuIds, ";");
					}
					if(null != menuIdArr && menuIdArr.length >0){
						if(null == roleMenuList || roleMenuList.size() ==0){//(1-1)页面有传值,  角色不存在菜单
							List<RoleMappMenu> roleMapMenuList = new ArrayList<RoleMappMenu>();
							for(String id : menuIdArr){
								if(!StringUtil.isEmpty(id)){
									RoleMappMenu roleMapp = new RoleMappMenu();
									roleMapp.setMenuId(Integer.parseInt(id));
									roleMapp.setRoleId(roleId);
									roleMapMenuList.add(roleMapp);
								}
							}
							/********批量新增角色菜单关系*********/
							sbs.batchSaveRoleMappMenu(roleMapMenuList);
							/******清空缓存中的角色菜单关系*****/
							DataCacheUtil.clearViewRoleMenuMap(); 
						}else{ //(1-2)页面有传值, 角色存在菜单
							boolean delFlag = false;
							List<RoleMappMenu> batchDelList = new ArrayList<RoleMappMenu>();
							for(ViewRoleMenu vrm : roleMenuList){ //(1-2-1)角色中有, 页面没有,则删除
								for(String id : menuIdArr){
									if(!StringUtil.isEmpty(id)){
										if(vrm.getMenuId().intValue() != Integer.parseInt(id)){
											delFlag = true;
										}else{
											delFlag = false;
											break;
										}
									}
								}
								if(delFlag){ 
									 RoleMappMenu rmm = new RoleMappMenu();
									 rmm.setId(vrm.getRoleMappMenuId());
									 batchDelList.add(rmm);
								}
							}
							/*********批量删除*****/
							if(batchDelList.size() > 0){
								 sbs.batchDeleteRoleMappMenu(batchDelList);
								 /******清空缓存中的角色菜单关系*****/
								 DataCacheUtil.clearViewRoleMenuMap();
							}
							 
							boolean batchFlag = false;
							List<RoleMappMenu> batchSaveList = new ArrayList<RoleMappMenu>();
							for(String id : menuIdArr){ //(1-2-2)角色没有, 页面有,则新增
								if(!StringUtil.isEmpty(id)){
									int menuId = Integer.parseInt(id);
									for(ViewRoleMenu vrm : roleMenuList){
										if(menuId != vrm.getMenuId()){
											batchFlag = true;
										}else{
											batchFlag = false;
											break;
										}
									}
									if(batchFlag){
										RoleMappMenu rmm = new RoleMappMenu();
										rmm.setMenuId(menuId);
										rmm.setRoleId(roleId);
										batchSaveList.add(rmm);
									}
								}
							}
							/******批量新增角色菜单关系*******/
							if(batchSaveList.size() > 0){
								sbs.batchSaveRoleMappMenu(batchSaveList);
								/******清空缓存中的角色菜单关系*****/
								 DataCacheUtil.clearViewRoleMenuMap();
							}
						}
					}else{ //页面传回菜单为空
						if(null != roleMenuList && roleMenuList.size()>0 ){//角色存在菜单时, 所以得批量删除
							List<RoleMappMenu> rmmList = new ArrayList<RoleMappMenu>();
							for(ViewRoleMenu vrm : roleMenuList){
								RoleMappMenu rmm = new RoleMappMenu();
								rmm.setId(vrm.getRoleMappMenuId());
								rmmList.add(rmm);
							}
							/*********批量删除角色菜单关系*******/
							sbs.batchDeleteRoleMappMenu(rmmList); 
							/******清空缓存中的角色菜单关系*****/
							 DataCacheUtil.clearViewRoleMenuMap();
						}
					}
					json.put("statusCode", "200");
					json.put("message", "修改角色成功!");
				}else{
					json.put("statusCode", "300");
					json.put("message", "系统繁忙, 修改角色失败!");
				}
			}
		} catch (Exception ex) {
			ex.printStackTrace();
			json.put("statusCode", "300");
			json.put("message", "系统异常, 修改角色失败!");
		}
		result = json.toString();
		return SUCCESS;
	}
}
